﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
using HTSwitchServerNamespace;
using HTSwitchFlyersNameSpace;


namespace HTMailSenderNamespace
{
    public class SimpleAsynchronousExample
    {
        static bool mailSent = false;
        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
             String token = (string) e.UserState;

            if (e.Cancelled)
            {
                CLogger.Append("Error #S1: Send Canceled: " + token + e.Cancelled.ToString(), CLogger.ERROR);
                 //Console.WriteLine("[{0}] Send canceled.", token);
            }
            if (e.Error != null)
            {
                CLogger.Append("Error #S2:: " + token + e.Error.ToString(), CLogger.ERROR);
                // Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
            } else
            {
                CLogger.Append("Mensaje enviado: ", CLogger.INFO);
               // Console.WriteLine("Message sent.");
            }
            mailSent = true;
        }
        public static void MailAlerta(string[] args)
        {
            // Command line argument must the the SMTP host.
            //SmtpClient client = new SmtpClient(args[0],Convert.ToInt32(args[1]));

            try
            {
                SmtpClient client = new SmtpClient(args[0]);
                client.Port = 25;

                //  MailAddress from = new MailAddress("fabio.dellarosa@zbctech.com.ar","Fabio " + (char)0xD8 + "Dellarosa", 
                //MailAddress from = new MailAddress("fabio.dellarosa@zbctech.com.ar","Fabio " + "Dellarosa",
            
                 MailAddress from = new MailAddress("alarma@horsetrack.com.ar","Alarma " + "Horsetrack",
            
                 System.Text.Encoding.UTF8);
                 client.UseDefaultCredentials = false;
                 client.Credentials = new System.Net.NetworkCredential("alarma@horsetrack.com.ar", "Horsetrack8");
                 client.EnableSsl = false;
                // Set destinations for the e-mail message.
                
                 MailAddress to = new MailAddress(args[2]);  //A quien va dirigido 
                 // MailAddress to = new MailAddress("dellarosafabio@gmail.com");

                // Specify the message content.
                MailMessage message = new MailMessage(from, to);
                message.Body = "Alarma Horsetrack";
                // Include some non-ASCII characters in body and subject.
                string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
                message.Body += Environment.NewLine + someArrows;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.Subject = "Alarma HorseTrack" + someArrows;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                // Set the method that is called back when the send operation ends.

                //client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);


                CLogger.Append("Sending message... .", CLogger.INFO);
                //Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
                //string answer = Console.ReadLine();
                // If the user canceled the send, and mail hasn't been sent yet,
                // then cancel the pending operation.
                if (mailSent == false)
                {
                    client.SendAsyncCancel();
                }
                

                client.Send(message);

                // Clean up.
                message.Dispose();

                CLogger.Append("Mensaje enviado.", CLogger.INFO);
                //Console.WriteLine("Goodbye.");
            }
            catch (Exception ex)
            {
                CLogger.Append("Error in MailSender: " + ex, CLogger.ERROR);
            }
        }

        public static void MailActivarUsuario(string[] args,string strSubject,string strMailFrom,string strMensaje )
        {
            // Command line argument must the the SMTP host.
            //SmtpClient client = new SmtpClient(args[0],Convert.ToInt32(args[1]));

            try
            {
                SmtpClient client = new SmtpClient(args[0]);
                client.Port = 25;

                //  MailAddress from = new MailAddress("fabio.dellarosa@zbctech.com.ar","Fabio " + (char)0xD8 + "Dellarosa", 
                //MailAddress from = new MailAddress("fabio.dellarosa@zbctech.com.ar","Fabio " + "Dellarosa",

                MailAddress from = new MailAddress(strMailFrom, "Activar User " + "Horsetrack",

                System.Text.Encoding.UTF8);
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential("activaruser@horsetrack.com.ar", "Activar8");
                // Set destinations for the e-mail message.

                MailAddress to = new MailAddress(args[2]);  //A quien va dirigido 
                // MailAddress to = new MailAddress("dellarosafabio@gmail.com");

                // Specify the message content.
                MailMessage message = new MailMessage(from, to);
                message.Body = strMensaje;
                 //"Gracias por elegir " + strProyecto + ". \n" + args[2] + " IMEI: "+ strIMEI+" no esta activo, \n Gracias - " + strMailFrom;
                //"El usuario IMEI: " + strIMEI + "- MAIL: "+strEmailDevice+"- no esta activo, \n Gracias - " + strMailFrom;
                // Include some non-ASCII characters in body and subject.
                string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
                message.Body += Environment.NewLine + someArrows;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.Subject = strSubject + someArrows;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                // Set the method that is called back when the send operation ends.

                //client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);


                CLogger.Append(strSubject+": Sending message... .", CLogger.INFO);
                //Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
                //string answer = Console.ReadLine();
                // If the user canceled the send, and mail hasn't been sent yet,
                // then cancel the pending operation.
                if (mailSent == false)
                {
                    client.SendAsyncCancel();
                }
                client.EnableSsl = false;
                // client.EnableSsl = true;

                client.Send(message);

                // Clean up.
                message.Dispose();

                CLogger.Append("Mensaje enviado.", CLogger.INFO);
                //Console.WriteLine("Goodbye.");
            }
            catch (Exception ex)
            {
                CLogger.Append("Error in MailSender: " + ex, CLogger.ERROR);
            }
        }

        public static void MailGmailHTML(string[] args, string strProyecto, string strMailFrom, string strMensaje)
        {
            // Command line argument must the the SMTP host.
            //SmtpClient client = new SmtpClient(args[0],Convert.ToInt32(args[1]));
            ///////////TO SEND
            //Aspose.Network.Mail.SmtpClient client = new SmtpClient("smtp.gmail.com");
            // set your Gmail username
            /*client.Username = "user@gmail.com";
            // set you Gmail password
            client.Password = "pwd";
            // set the port to 587. This is the SSL port of Gmail SMTP server
            client.Port = 587;
            // set the security mode to explicit
            client.SecurityMode = SmtpSslSecurityMode.Explicit;
            // enable SSL
            client.EnableSsl = true; 
            */
            try
            {
                SmtpClient client = new SmtpClient(args[0]);
                client.Port = 587;

                //  MailAddress from = new MailAddress("fabio.dellarosa@zbctech.com.ar","Fabio " + (char)0xD8 + "Dellarosa", 
                //MailAddress from = new MailAddress("fabio.dellarosa@zbctech.com.ar","Fabio " + "Dellarosa",

                MailAddress from = new MailAddress(strMailFrom, "Atencion: Datos de usuario " + "Control Total",System.Text.Encoding.UTF8);
                client.EnableSsl = true;
                //client.EnableSsl = false;                 
                client.UseDefaultCredentials = false;
                                
                client.Credentials = new System.Net.NetworkCredential("controlesyseguimiento@gmail.com", "fabiohernan");

                
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                // Set destinations for the e-mail message.
                MailAddress to = new MailAddress(args[2]);  //A quien va dirigido 
                // MailAddress to = new MailAddress("dellarosafabio@gmail.com");    //A QUIEN VA DIRIGIDO
                CLogger.Append("DESTINATARIO: " + to, CLogger.INFO);    //FD DEBUG
                // Specify the message content.
                MailMessage message = new MailMessage(from, to);
                
                
                message.Priority = MailPriority.High; 
                message.Body = strMensaje;
                
                   //htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
                //message.AlternateViews.Add(htmlView);

                //"Gracias por elegir " + strProyecto + ". \n" + args[2] + " IMEI: "+ strIMEI+" no esta activo, \n Gracias - " + strMailFrom;
                //"El usuario IMEI: " + strIMEI + "- MAIL: "+strEmailDevice+"- no esta activo, \n Gracias - " + strMailFrom;
                // Include some non-ASCII characters in body and subject.
                string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });


                message.Body += Environment.NewLine + someArrows;
                
                message.BodyEncoding = System.Text.Encoding.UTF8;

                message.IsBodyHtml = true; 
                message.Subject = "Atencion: Datos de usuario " + strProyecto + someArrows;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                // Set the method that is called back when the send operation ends.

                //client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
                

                CLogger.Append("Sending message... .", CLogger.INFO);
                //Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
                //string answer = Console.ReadLine();
                // If the user canceled the send, and mail hasn't been sent yet,
                // then cancel the pending operation.
                if (mailSent == false)
                {
                    client.SendAsyncCancel();
                }
                
                
                client.Send(message);

                // Clean up.
                message.Dispose();

                CLogger.Append("Mensaje enviado.", CLogger.INFO);
                //Console.WriteLine("Goodbye.");
            }
            catch (Exception ex)
            {
                CLogger.Append("Error in MailSender: " + ex, CLogger.ERROR);
                throw new Exception("ERROR AL ENVIAR MAIL SENDER: " + ex.Message);
                   //
            }
        }


        public void enviarAlarmaServicioSinMoviles(object mobile)
        {

            string strMobile = (string)mobile;
            string[] strClientHostDatos = new string[5];
            string strMensaje;

            try
            {
                strMensaje = "<br />ALARMA CONTROL SATELITAL <br /><br /> No se detecta una nueva locación desde hace 2 horas del movil: ."+ strMobile;

                strClientHostDatos[0] = "smtp.gmail.com";
                strClientHostDatos[1] = "587";
                //strClientHostDatos[2] = "futuroseguridad@gmail.com";///MAIL DE DESTINO A QUIEN VA IRIGIDO
                strClientHostDatos[2] = "controlesyseguimiento@gmail.com";///MAIL DE DESTINO A QUIEN VA IRIGIDO
                //strClientHost[2] = objGPSAndroidData.EMAIL;   //Dirección de mail a quien se le va a enviar
                HTMailSenderNamespace.SimpleAsynchronousExample.MailGmailHTML(strClientHostDatos, "Alarma cys Movil", "controlesyseguimiento@gmail.com", strMensaje);

                //MAIL COPIA
                strClientHostDatos[2] = "controlesyseguimiento@gmail.com";///MAIL DE DESTINO A QUIEN VA IRIGIDO
                HTMailSenderNamespace.SimpleAsynchronousExample.MailGmailHTML(strClientHostDatos, "Alarma cys Movil", "controlesyseguimiento@gmail.com", strMensaje);
            }catch(Exception ex)
            {
                throw new Exception("[enviarAlarmaServicioSinMoviles] ERROR AL ENVIAR MAIL: " + ex.Message);
            }
            
        }

        #region ENVIO DE MAIL GMAIL
        public static void envioDeMailGmail(string[] args, string strProyecto, string strMailFrom, string strMensaje)
        {
            // Command line argument must the the SMTP host.
            //SmtpClient client = new SmtpClient(args[0],Convert.ToInt32(args[1]));
            ///////////TO SEND
            //Aspose.Network.Mail.SmtpClient client = new SmtpClient("smtp.gmail.com");
            // set your Gmail username
            /*client.Username = "user@gmail.com";
            // set you Gmail password
            client.Password = "pwd";
            // set the port to 587. This is the SSL port of Gmail SMTP server
            client.Port = 587;
            // set the security mode to explicit
            client.SecurityMode = SmtpSslSecurityMode.Explicit;
            // enable SSL
            client.EnableSsl = true; 
            */
            try
            {
                SmtpClient client = new SmtpClient(args[0]);
                client.Port = 587;

                //  MailAddress from = new MailAddress("fabio.dellarosa@zbctech.com.ar","Fabio " + (char)0xD8 + "Dellarosa", 
                //MailAddress from = new MailAddress("fabio.dellarosa@zbctech.com.ar","Fabio " + "Dellarosa",

                MailAddress from = new MailAddress(strMailFrom, "Atencion: Datos de usuario " + "Control Total", System.Text.Encoding.UTF8);
                client.EnableSsl = true;
                //client.EnableSsl = false;                 
                client.UseDefaultCredentials = false;

                client.Credentials = new System.Net.NetworkCredential("controlesyseguimiento@gmail.com", "fabiohernan");


                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                // Set destinations for the e-mail message.
                MailAddress to = new MailAddress(args[2]);  //A quien va dirigido 
                // MailAddress to = new MailAddress("dellarosafabio@gmail.com");    //A QUIEN VA DIRIGIDO
                CLogger.Append("DESTINATARIO: " + to, CLogger.INFO);    //FD DEBUG
                // Specify the message content.
                MailMessage message = new MailMessage(from, to);


                message.Priority = MailPriority.High;
                message.Body = strMensaje;

                //htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
                //message.AlternateViews.Add(htmlView);

                //"Gracias por elegir " + strProyecto + ". \n" + args[2] + " IMEI: "+ strIMEI+" no esta activo, \n Gracias - " + strMailFrom;
                //"El usuario IMEI: " + strIMEI + "- MAIL: "+strEmailDevice+"- no esta activo, \n Gracias - " + strMailFrom;
                // Include some non-ASCII characters in body and subject.
                string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });


                message.Body += Environment.NewLine + someArrows;

                message.BodyEncoding = System.Text.Encoding.UTF8;

                message.IsBodyHtml = true;
                message.Subject = "Atencion: Datos de usuario " + strProyecto + someArrows;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                // Set the method that is called back when the send operation ends.

                //client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);


                CLogger.Append("Sending message... .", CLogger.INFO);
                //Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
                //string answer = Console.ReadLine();
                // If the user canceled the send, and mail hasn't been sent yet,
                // then cancel the pending operation.
                if (mailSent == false)
                {
                    client.SendAsyncCancel();
                }


                client.Send(message);

                // Clean up.
                message.Dispose();

                CLogger.Append("Mensaje enviado.", CLogger.INFO);
                //Console.WriteLine("Goodbye.");
            }
            catch (Exception ex)
            {
                CLogger.Append("Error in MailSender: " + ex, CLogger.ERROR);
                throw new Exception("ERROR AL ENVIAR MAIL SENDER: " + ex.Message);
                //
            }
        }

        #endregion

    }

}




                                                                                      
